package com.wpw.web.work.sql;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @ClassName: LocationCodeSql
 * @CreateDate: 2020-05-06 22:40
 * @author: wupengwei
 */
public class LocationCodeSql {

    //INSERT INTO db_procurement.t_location_product_code(`location_id`,`location_code`, `product_id`, `code`,`status`,`operator_id`,`operator_name`) VALUES ((SELECT id FROM t_location WHERE `status` = 0 AND location_code =
    // 'BJ-HQYD-A-1-1'),'BJ-YDT-DO-1-4', 17064, '12DJ0Y2', 0, 0 ,'admin');
    public static void main(String[] args) {

        String filePath = "/Users/wupengwei/Desktop/储位数据/sn储位信息.txt";
        Path path = Paths.get(filePath);
        try {
            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
            int totalSize = lines.size();
            System.out.println("共" + totalSize + "条数据");

            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < lines.size(); i++) {
                String data = lines.get(i);
                String[] split = data.split("\t");
                List<String> list = Arrays.asList(split);
                dataList.add(list);
            }

            String jsFile = "/Users/wupengwei/Desktop/储位数据/locaiontCode.sql";
            Path jsPath = Paths.get(jsFile);
            BufferedWriter writer = Files.newBufferedWriter(jsPath, StandardCharsets.UTF_8);
            for (List<String> list : dataList) {
                String locationCode = list.get(0);
                String productId = list.get(1);
                String code = list.get(2);
                StringBuilder sb = new StringBuilder("INSERT INTO db_procurement.t_location_product_code(`location_id`,`location_code`, `product_id`, `code`,`status`,`operator_id`,`operator_name`) VALUES ((SELECT id FROM t_location WHERE `status` = 0 AND location_code = '")
                        .append(locationCode).append("'),'")
                        .append(locationCode).append("', ")
                        .append(productId).append(", '")
                        .append(code).append("', 0, 0 ,'admin');");

                writer.write(sb.toString());
                writer.newLine();

            }

            writer.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}